//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package 力扣.题库.未分类.demo40AND60;

import java.util.ArrayList;
import java.util.List;

public class demo_54 {
    public demo_54() {
    }

    public static void main(String[] args) {
        int[][] matrix = new int[][]{{1, 11}, {2, 0}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}};
        System.out.println(spiralOrder(matrix));
    }

    public static List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> list = new ArrayList();
        int x = 0;
        int y = 0;
        int direction = 0;

        while(list.size() < matrix.length * matrix[0].length) {
            list.add(matrix[x][y]);
            matrix[x][y] = Integer.MAX_VALUE;
            switch (direction) {
                case 0:
                    if (y == matrix[x].length - 1 || matrix[x][y + 1] == Integer.MAX_VALUE) {
                        ++direction;
                    }
                    break;
                case 1:
                    if (x == matrix.length - 1 || matrix[x + 1][y] == Integer.MAX_VALUE) {
                        ++direction;
                    }
                    break;
                case 2:
                    if (y == 0 || matrix[x][y - 1] == Integer.MAX_VALUE) {
                        ++direction;
                    }
                    break;
                case 3:
                    if (x == 0 || matrix[x - 1][y] == Integer.MAX_VALUE) {
                        direction = 0;
                    }
            }

            switch (direction) {
                case 0:
                    ++y;
                    break;
                case 1:
                    ++x;
                    break;
                case 2:
                    --y;
                    break;
                case 3:
                    --x;
            }
        }

        return list;
    }
}
